#include "widget.h"
#include "ui_widget.h"

#include <QListWidget>
#include <QListWidgetItem>
#include <QLineEdit>
#include <QPushButton>
#include <QDebug>
#include <QTableWidget>
#include <QTreeWidget>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QTreeWidget* tree=new QTreeWidget(this);
    tree->setFixedSize(400,200);

    tree->setHeaderLabel("动物");
    QTreeWidgetItem* item1 = new QTreeWidgetItem();
    item1->setText(0, "猫");
    tree->addTopLevelItem(item1);
    QTreeWidgetItem* item11 = new QTreeWidgetItem();
    item11->setText(0, "白猫");
    item1->addChild(item11);
    QTreeWidgetItem* item12 = new QTreeWidgetItem();
    item12->setText(0, "黑猫");
    item1->addChild(item12);

    QTreeWidgetItem* item2 = new QTreeWidgetItem();
    item2->setText(0, "狗");
    tree->addTopLevelItem(item2);

    QTreeWidgetItem* item3 = new QTreeWidgetItem();
    item3->setText(0, "鸟");
    tree->addTopLevelItem(item3);

    /*
    QTableWidget* table=new QTableWidget(this);
    table->setFixedSize(400,200);

    // 创建 3 ⾏
    table->insertRow(0);
    table->insertRow(1);
    table->insertRow(2);
    // 创建 3 列
    table->insertColumn(0);
    table->insertColumn(1);
    table->insertColumn(2);
    // 给 3 列设定列名
    table->setHorizontalHeaderItem(0, new QTableWidgetItem("学号"));
    table->setHorizontalHeaderItem(1, new QTableWidgetItem("姓名"));
    table->setHorizontalHeaderItem(2, new QTableWidgetItem("年龄"));
    // 设置初始数据
    table->setItem(0, 0, new QTableWidgetItem("1001"));
    table->setItem(0, 1, new QTableWidgetItem("张三"));
    table->setItem(0, 2, new QTableWidgetItem("20"));
    table->setItem(1, 0, new QTableWidgetItem("1002"));
    table->setItem(1, 1, new QTableWidgetItem("李四"));
    table->setItem(1, 2, new QTableWidgetItem("21"));
    table->setItem(2, 0, new QTableWidgetItem("1003"));
    table->setItem(2, 1, new QTableWidgetItem("王五"));
    table->setItem(2, 2, new QTableWidgetItem("19"));
    */

    /*
    QListWidget* list=new QListWidget(this);
    list->setFixedSize(100,200);

    list->addItem("选项1");
    list->addItem("选项2");
    list->addItem("选项3");

    QLineEdit* edit=new QLineEdit(this);
    edit->move(150,50);

    QPushButton* btn1=new QPushButton(this);
    btn1->setText("添加");
    btn1->move(130,100);

    QPushButton* btn2=new QPushButton(this);
    btn2->setText("删除");
    btn2->move(220,100);

    connect(btn1,&QPushButton::clicked,this,[=](){
        // 获取编辑区中的文字
        const QString& text = edit->text();
        if (text.isEmpty())
        {
            return;
        }
        list->addItem(text);
        qDebug()<<"添加："<<text;
    });

    connect(btn2,&QPushButton::clicked,this,[=](){
        // 获取当前被选中的元素
        int row = list->currentRow();
        qDebug()<<"删除："<<list->item(row);
        // 删除这一行
        list->takeItem(row);
    });
    */
}

Widget::~Widget()
{
    delete ui;
}
